<form lay-filter="agentLevelForm" class="layui-form open-form">
    <div class="open-model">
        <div class="open-auto">
            <input name="id" type="hidden"/>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>级别名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入级别名称" type="text" class="layui-input" maxlength="200"
                           lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>级别编号</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入级别编号" type="text" class="layui-input" maxlength="200"
                           lay-verify="required|isOnly" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>级别描述</label>
                <div class="layui-input-block">
                    <input name="remark" placeholder="请输入级别描述" type="text" class="layui-input" maxlength="200"
                           lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>代理资格费</label>
                <div class="layui-input-block">
                    <input name="agentFee" placeholder="请输入代理资格费" type="text" class="layui-input" maxlength="10"
                           lay-verify="required|decimal" required/>
                    <span class="prompt">提示：单位(元)</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>保证金</label>
                <div class="layui-input-block">
                    <input name="earnestMoney" placeholder="请输入保证金" type="text" class="layui-input" maxlength="10"
                           lay-verify="required|decimal" required/>
                    <span class="prompt">提示：单位(元)</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>价格折扣</label>
                <div class="layui-input-block">
                    <input name="discount" placeholder="请输入价格折扣" type="text" class="layui-input" maxlength="10"
                           lay-verify="required|decimal|discount" required/>
                    <span class="prompt">提示：输入范围(0-1)；例：输入0.6表示6折，列表显示为(60%)</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">要求</label>
                <div class="layui-input-block">
                    <textarea id="askFor" name="askFor" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支持政策</label>
                <div class="layui-input-block">
                    <textarea id="supportPolicy" name="supportPolicy" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="agentLevelFormSubmit" lay-submit>保存</button>
        </div>
    </div>
</form>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'layedit'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;


        //查询企业信息
        var enterpriseInfo;

        admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
            enterpriseInfo = res.data;
        }, 'get', false);
        if ("" == enterpriseInfo || null == enterpriseInfo) {
            layer.msg("企业信息获取异常", {icon: 2});
            return;
        }
        // 回显代理商级别数据
        var data = admin.getTempData('agentLevel');

        //自定义验证规则
        form.verify({
            isOnly: function (value) {
                var isOnly = true;
                var idVal = $("form input[name='id']").val();
                admin.req('enterprise/sell/admin/agentLevel/isOnly', {
                    code: value,
                    id: idVal
                }, function (res) {
                    if (res.code == 200) {
                        isOnly = res.data;
                    }
                }, 'GET', false);
                if (!isOnly) {
                    return "级别编号必须唯一"
                }
            },
            decimal: [/^[0-9]*(\.[0-9]{1,2})?$/, '格式错误'],
            discount: function (value) {
                if (value < 0 || value > 1) {
                    return "价格折扣可输入范围(0-1)";
                }
            }
        })

        var layeditIndex13 = layedit.build('askFor', {
            uploadImage: {
                url: config.upload_url + 'uploadFile',
                type: 'post',
                data: {"path": enterpriseInfo.enterpriseCode + "/"},
                base: config.base,
                before: function (obj) {
                    layer.load(); //上传loading
                    this.headers = {Authorization: 'Bearer ' + config.getToken()};
                    this.data = {"path": enterpriseInfo.enterpriseCode + "/"};
                }
            }
        });

        var layeditIndex14 = layedit.build('supportPolicy', {
            uploadImage: {
                url: config.upload_url + 'uploadFile',
                type: 'post',
                data: {"path": enterpriseInfo.enterpriseCode + "/"},
                base: config.base,
                before: function (obj) {
                    layer.load(); //上传loading
                    this.headers = {Authorization: 'Bearer ' + config.getToken()};
                    this.data = {"path": enterpriseInfo.enterpriseCode + "/"};
                }
            }
        });
        if (data !== undefined) {
            layedit.setContent(layeditIndex13, data.askFor, false);
            layedit.setContent(layeditIndex14, data.supportPolicy, false);
        }
        form.val('agentLevelForm', data === undefined ? null : data);

        // 表单提交事件
        form.on('submit(agentLevelFormSubmit)', function (d) {
            layer.load(2);
            d.field.askFor = layedit.getContent(layeditIndex13);
            d.field.supportPolicy = layedit.getContent(layeditIndex14);
            admin.req(data ? 'enterprise/sell/admin/agentLevel/update' : 'enterprise/sell/admin/agentLevel/add', d.field, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('agentLevelTable');
                    layer.closeAll('page');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, data ? 'PUT' : 'POST');
            return false;
        });

    });
</script>